package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private CheckGroupDao checkgroupDao;
    @Autowired
    private CheckItemDao checkItemDao;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckGroup(setmealId,checkgroupIds);
    }





    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        return getById(id);
    }



    public void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds){
        if (checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }


    private Setmeal getById(int id) {
        Setmeal setmeal = setmealDao.getById(id);
        int[] ids = new int[]{id};
        for (int setmealId : ids) {
            //查询出该套餐所拥有的检查组
            List<CheckGroup> checkGroups =  checkgroupDao.listBySetmealId(setmealId);
            for (CheckGroup checkGroup : checkGroups) {
                //查询出该检查组所拥有的的检查项
                List<CheckItem> checkItems = checkItemDao.findCheckItemByCheckGroupId(checkGroup.getId());
                //把检查项封装进检查组中
                checkGroup.setCheckItems(checkItems);
            }
            //把检查组封装进套餐中
            setmeal.setCheckGroups(checkGroups);
        }
        return setmeal;
    }

    /**
     * 查询套餐占比数据
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {

        return setmealDao.findSetmealCount();
    }

    @Override
    public List<Map<String, Object>> findSetmealCount(List<Integer> setmeal_id) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Integer seymealId : setmeal_id) {
            Map<String, Object> setmealCountBydata = setmealDao.findSetmealCountBydata(seymealId);
            System.out.println(setmealCountBydata);
            for (Map<String, Object> map : list) {
                if (map.get("name").equals(setmealCountBydata.get("name"))){
                    int i = (int) (Double.parseDouble(map.get("value").toString()) + Double.parseDouble(setmealCountBydata.get("value").toString()));
                    setmealCountBydata.put("value",i);
                }
            }
            list.add(setmealCountBydata);
        }
        return list;
    }
}
